﻿using System;
using System.Text;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Campus
{
    static class ResolutionHandler
    {
        public static int WindowWidth = 800;
        public static int WindowHeight = 600;

        public static int MinimumWidth = 800;
        public static int MinimumHeight = 300;

        /// <summary>
        /// This is used to make the GUI rearrange itself for the new resolution.  It should be
        /// changed whenever the graphics manager has its back buffer changed.
        /// </summary>
        public static bool GUINeedsReloading = false;

        /// <summary>
        /// A static method for changing the resolution of the window and toggling fullscreen.
        /// </summary>
        /// <param name="resolution">The new resolution which is a string formatted like "800*600" where the
        /// width is first and the height is second and only 1 asterisk and whitespace (as much as wanted, or none
        /// at all) seperate the two.</param>
        /// <param name="level">The level that needs its viewable area changed</param>
        /// <param name="graphics">The GraphicsDeviceManager that needs its back buffer changed</param>
        public static void ChangeResolution(string resolution, Level level, GraphicsDeviceManager graphics)
        {
            if (resolution == "Native")
            {
                SetResolutionToNative(graphics);
                return;
            }

            string[] splitResolution = resolution.Split('*');

            // If there aren't 2 parameters, return
            if (splitResolution.Length != 2)
            {
                return;
            }

            int width, height;

            bool success = Int32.TryParse(splitResolution[0].Trim(), out width);

            // If there was a parsing error or a negative value, return
            if (!success || width <= 0)
            {
                return;
            }

            success = Int32.TryParse(splitResolution[1].Trim(), out height);

            // If there was a parsing error or a negative value, return
            if (!success || height <= 0)
            {
                return;
            }

            // Don't go through all the trouble if the resolution passed in is the
            // same as it is now (won't invoke the graphics manager and won't try to
            // recenter all the GUI elements)
            if (width == WindowWidth && height == WindowHeight)
            {
                return;
            }

            bool goFullScreen = false;
            if (graphics.IsFullScreen)
            {
                graphics.ToggleFullScreen();
                goFullScreen = true;
            }

            // Change the static variables
            WindowWidth = width;
            WindowHeight = height;

            // Change the device manager's back buffer
            graphics.PreferredBackBufferWidth = width;
            graphics.PreferredBackBufferHeight = height;

            // Apply the changes (won't do anything without this)
            graphics.ApplyChanges();

            if (goFullScreen == true)
            {
                graphics.ToggleFullScreen();
            }

            GUINeedsReloading = true;
        }

        public static void SetResolutionToNative(GraphicsDeviceManager graphics)
        {
            bool goFullScreen = false;
            if (graphics.IsFullScreen)
            {
                graphics.ToggleFullScreen();
                goFullScreen = true;
            }

            WindowWidth = graphics.PreferredBackBufferWidth = graphics.GraphicsDevice.DisplayMode.Width;
            WindowHeight = graphics.PreferredBackBufferHeight = graphics.GraphicsDevice.DisplayMode.Height;

            graphics.ApplyChanges();

            if (goFullScreen == true)
            {
                graphics.ToggleFullScreen();
            }

            GUINeedsReloading = true;
        }

        public static void ToggleFullscreen(GraphicsDeviceManager graphics)
        {
            GUINeedsReloading = true;

            graphics.ToggleFullScreen();
        }
    }
}
